﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Castle.MonoRail.Framework;
using StateTheaterDataModel;
using System.Web.Security;
using Castle.MonoRail.Framework.ViewComponents;
using Castle.MonoRail.ViewComponents;

namespace StateTheater.Component
{
	public class ActorWatchComponent : ViewComponent 
	{
		public StateTheaterDataContext Db { get; set; }
		private bool displayAW;
		private MembershipUser user;

		public override void Initialize()
		{
			base.Initialize();
			user = Membership.GetUser();
			displayAW = user != null && this.Request.ReadCookie("DisplayedActorWatch") == null ;
		}

		public override void Render()
		{
			CancelView();
			if (!displayAW)
				return;
			base.Render();

			int memberId = (int)user.ProviderUserKey;
			var start = DateTime.Now.AddDays(6);
			var enddate = DateTime.Now;		// .AddDays(-1600);

			var people = (
			from a in Db.ActorWatches
			join r in Db.Roles on a.PersonID equals r.PersonID
			where a.MEMBER_ID == memberId
			 && r.Production.FirstPerformance < start
			 && r.Production.LastPerformance > enddate
			// select new { r.Production.Play.Title, r.People.FullName}
			select r.People.FullName
			).Concat(
			from a in Db.ActorWatches
			join r in Db.ProductionCredits on a.PersonID equals r.PersonID
			where a.MEMBER_ID == memberId
			 && r.Production.FirstPerformance < start
			 && r.Production.LastPerformance > enddate
			// select new { r.Production.Play.Title, r.People.FullName}
			select r.People.FullName
			).Concat(
			from a in Db.ActorWatches
			join r in Db.ProductionCredits on a.PersonID equals r.PersonID
			where a.MEMBER_ID == memberId
			 && r.Production.FirstPerformance < start
			 && r.Production.LastPerformance > enddate
			// select new { r.Production.Play.Title, r.People.FullName}
			select r.People.FullName
			).Distinct().ToList();

			var cnt = people.Count;
				string msg;
				var person = people.FirstOrDefault();
			this.Response.CreateCookie("DisplayedActorWatch","true");
			CancelView();
			switch (cnt)
			{
				case 0:
					return;
					break;

				case 1:
					msg = "{0} is on stage this week.  ";
					break;

				case 2:
					msg = "{0} + one other person you're watching are on stage this week.";
					break;

				default:
					msg = "{0} + {1} others you're watching are on stage this week.  ";
					break;
			}
			msg += "<a href='{2}/ActorWatch/Display.rails'>Click  here for more.</a>";
			msg = String.Format(msg, person, cnt-1, PropertyBag["siteRoot"]);
			var jh = new JavascriptHelper(this.Context, this.EngineContext, "actorwatch");
			jh.IncludeStandardScripts("notify");

			jh.IncludeOnReadyText(Environment.NewLine 
					+ @"$('#StatusBar').jnotifyAddMessage({{text: ""{0}"", permanent: true, type: 'message'}});" 
					+ Environment.NewLine, msg);

			CancelView();
			this.Context.ViewToRender = null;
			RenderText("");

		}
	}
}
